package com.tarzan.common.core.json;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;

/**
 * @Package com.tarzan.common.core.json
 * @Title OverNullJsonMapper
 * @Description TODO 将null  转化为 “”
 * @Author liujinshan
 * @CreateTime 2021/7/20 8:59
 */
public class OverNullJsonMapper  extends ObjectMapper {
    private static final long serialVersionUID = 1L;

    public OverNullJsonMapper() {
        // this(Include.NON_EMPTY);
        // 空值处理为空串
        this.getSerializerProvider().setNullValueSerializer(
                new JsonSerializer<Object>() {
                    @Override
                    public void serialize(Object value, JsonGenerator jgen,
                                          SerializerProvider provider) throws IOException,
                            JsonProcessingException {
                        jgen.writeString("");
                    }
                });
    }
}
